package ink.ykb.pdfdemo.config;
 
import com.itextpdf.kernel.colors.DeviceGray;
import com.itextpdf.kernel.events.Event;
import com.itextpdf.kernel.events.IEventHandler;
import com.itextpdf.kernel.events.PdfDocumentEvent;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.layout.Canvas;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.property.TextAlignment;
import com.itextpdf.layout.property.VerticalAlignment;
 
public class WatermarkingEventHandler implements IEventHandler {
	
	    private PdfFont pdfFont;
	
		public WatermarkingEventHandler(PdfFont pdfFont) {
			this.pdfFont = pdfFont;
		}
	
        @Override
        public void handleEvent(Event event) {
        	PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
            PdfDocument pdfDoc = docEvent.getDocument();
            PdfPage page = docEvent.getPage();
            PdfCanvas pdfCanvas = new PdfCanvas(
                page.newContentStreamBefore(), page.getResources(), pdfDoc);
	    	
            //watermark
//            Canvas canvas = new Canvas(pdfCanvas, pdfDoc, page.getPageSize())
//            .setFontColor(DeviceGray.makeLighter(new DeviceGray(0.6f)))
//            .setFontSize(60)
//            .setFont(pdfFont)
//            .showTextAligned(new Paragraph("IBEETL.COM"),
//            	298, 421, pdfDoc.getPageNumber(page),
//                TextAlignment.CENTER, VerticalAlignment.MIDDLE, 45);
            
            Canvas canvas = new Canvas(pdfCanvas, pdfDoc, page.getPageSize())
                .setFontColor(DeviceGray.makeLighter(new DeviceGray(0.6f)))
                .setFontSize(16)
                .setFont(pdfFont)
                .showTextAligned(new Paragraph("IBEETL.COM"),
                    32, 10, pdfDoc.getPageNumber(page),
                    TextAlignment.LEFT, VerticalAlignment.BOTTOM, 0);
        
        }
}
